package dice.sinanya.db.system;

import dice.sinanya.db.tools.DbUtil;
import dice.sinanya.entity.EntityTypeMessages;
import dice.sinanya.exceptions.NotProipertiesSwitch;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import static dice.sinanya.system.MessagesSystem.loginInfo;

public class SetPropertiesSwitch {
    private static final Logger log = LogManager.getLogger(SetPropertiesSwitch.class.getName());

    /**
     * 读取数据库中的骰点历史信息到缓存
     */
    public void setSystemColumn(String column, Object input, EntityTypeMessages entityTypeMessages) throws NotProipertiesSwitch {
        try (Connection conn = DbUtil.getConnection()) {
            //language=MySQL
            String sql = "update test.systemProperties set " + column + "=? where botId=?";
            try (PreparedStatement ps = conn.prepareStatement(sql)) {
                ps.setObject(1, input);
                ps.setString(2, String.valueOf(loginInfo.getLoginId()));
                ps.executeUpdate();
            }
        } catch (SQLException e) {
            throw new NotProipertiesSwitch(entityTypeMessages, e.getMessage());
//            log.error(e.getMessage(), e);
        }
    }

    /**
     * 读取数据库中的骰点历史信息到缓存
     */
    public void setBanColumn(String column, Object input, EntityTypeMessages entityTypeMessages) throws NotProipertiesSwitch {
        try (Connection conn = DbUtil.getConnection()) {
            //language=MySQL
            String sql = "update test.banProperties set " + column + "=? where botId=?";
            try (PreparedStatement ps = conn.prepareStatement(sql)) {
                ps.setObject(1, input);
                ps.setString(2, String.valueOf(loginInfo.getLoginId()));
                ps.executeUpdate();
            }
        } catch (SQLException e) {
            throw new NotProipertiesSwitch(entityTypeMessages, e.getMessage());
//            log.error(e.getMessage(), e);
        }
    }

    /**
     * 读取数据库中的骰点历史信息到缓存
     */
    public void setGameColumn(String column, Object input, EntityTypeMessages entityTypeMessages) throws NotProipertiesSwitch {
        try (Connection conn = DbUtil.getConnection()) {
            //language=MySQL
            String sql = "update test.gameProperties set " + column + "=? where botId=?";
            try (PreparedStatement ps = conn.prepareStatement(sql)) {
                ps.setObject(1, input);
                ps.setString(2, String.valueOf(loginInfo.getLoginId()));
                ps.executeUpdate();
            }
        } catch (SQLException e) {
            throw new NotProipertiesSwitch(entityTypeMessages, e.getMessage());
//            log.error(e.getMessage(), e);
        }
    }
}
